<template>
	<div>
    <div class="layer_top">
      <div class="name-area"><span class="names">用户姓名：</span>{{ dataBus.userName }}</div>
      <div class="name-area"><span class="names">性别：</span>{{ dataBus.userSex }}</div>
      <div class="name-area"><span class="names">联系电话：</span>{{ dataBus.userMobile }}</div>
      <div class="name-area"><span class="names">岗位角色：</span>{{ dataBus.roleName }}</div>
      <div class="name-area"><span class="names">民族：</span>{{ dataBus.userNation }}</div>
      <div class="name-area"><span class="names">政治面貌：</span>{{ dataBus.userPolity }}</div>
    </div>
    <hr style="margin: 15px 0">
    <Form>
        <FormItem label="请选择岗位角色">
          <Select v-model="dataForm.userRoleId" style="width:200px">
            <Option
              v-for="(item, index) in postRoleArray"
              :key="index"
              :value="item.userRoleId">{{ item.roleName }}
            </Option>
          </Select>
        </FormItem>
        <FormItem label="请选择权限类别">
          <CheckboxGroup v-model="bizRoleSelector">
            <Checkbox v-for="(item, index) in bizRoleArray" :key="index" :label="item.bizRoleCode">
              <span>{{item.bizRoleName}}</span>
            </Checkbox>
          </CheckboxGroup>
        </FormItem>
        <FormItem label="行政区划">
          <Cascader style="width: 500px;display:inline-block" :data="regionArray" v-model="regionModel" @on-change="regionSelector" :load-data="loadRegion"></Cascader>
        </FormItem>
        <FormItem v-show="showGroup" label="请选择村组">
          <CheckboxGroup v-model="villageGroupSelector">
            <Checkbox v-for="(item, index) in villageGroupArray" :key="index" :label="item.villageGroupId">
              <span>{{item.villageGroupName}}</span>
            </Checkbox>
          </CheckboxGroup>
        </FormItem>
      </Form>
    <Button type="primary" @click="Submit">提交</Button>
  </div>  
</template>

<script scope>
export default {
  props: {
    dataBus: Object,
    callbank: Function
  },
  data () {
    return {
      userRoleEnum: '18079012',
      showGroup: true,
      regionModel: [],
      regionArray: [],
      postRoleArray: [],
      bizRoleArray: [],
      bizRoleSelector: [],
      villageGroupArray: [],
      villageGroupSelector: [],
      dataForm: {
        userRoleId: null,
        userPlus: {}
      }
    }
  },
  methods: {
    Init() {
      this.getPostRole()
      this.getBizRole()
      this.regionInit()
      this.$nextTick(() => {
        this.dataForm.userRoleId = this.dataBus.userRoleId
        //勾选业务角色
        if(this.dataBus.authCode) {
          this.bizRoleSelector = this.dataBus.authCode.split(',')
        }
        //勾选包组范围
        if(this.dataBus.segmentArea) {
          this.villageGroupSelector = this.dataBus.segmentArea.split(',')
        }
        if(this.dataBus.provinceCode) {
          this.regionModel.push(this.dataBus.provinceCode)
        }
        if(this.dataBus.cityCode) {
          this.regionModel.push(this.dataBus.cityCode)
        }
        if(this.dataBus.countyCode) {
          this.regionModel.push(this.dataBus.countyCode)
        }
        if(this.dataBus.townshipCode) {
          this.regionModel.push(this.dataBus.townshipCode)
        }
        if(this.dataBus.villageCode) {
          this.regionModel.push(this.dataBus.villageCode)
        }
        if(this.dataBus.villageCode) {
          this.villageGroupInit(this.dataBus.villageCode)
        }
      })
    },
    //授权角色
    Submit(){
      this.dataForm.userId = this.dataBus.userId
      if(this.bizRoleSelector.length > 0) {
        this.dataForm.authCode = this.bizRoleSelector.toString()
      }
      if(this.villageGroupSelector.length > 0) {
        this.dataForm.userPlus.segmentArea = this.villageGroupSelector.toString()
      }
      new this.$rest({
        gateway: 'agrobiz',
        service: "agrobiz-uc",
        url: "/v1.0/user-src/update/union/user",
        data: {
          pojo: this.dataForm
        },
        action: (res) => {
          if (res.data.code === 20000) {
            this.$Message.success({
              content: '授权成功',
              duration: 1,
              onClose: ()=> {
                this.callbank()
              }
            })
          } else {
            this.$Notice.warning({
              desc: `失败原因：${res.data.msg}`,
            });
          }
        },
      }).post()
    },
    getPostRole() {
      new this.$rest({
        gateway: 'agrobiz',
        service: 'agrobiz-uc',
        url: '/v1.0/user/role/page',
        query: {
          pageIndex: 1,
          pageSize: 30
        },
        action: (res) => {
          if (res.data.code === 20000) {
            this.postRoleArray = res.data.data.list
          } else {
            this.$Message.error(res.data.msg)
          }
        }
      }).get()
    },
    getBizRole() {
      new this.$rest({
        gateway: 'agrobiz',
        service: 'agrobiz-uc',
        url: '/v1.0/biz/role/page',
        query: {
          pageIndex: 1,
          pageSize: 30
        },
        action: (res) => {
          if (res.data.code === 20000) {
            this.bizRoleArray = res.data.data.list
          } else {
            this.$Message.error(res.data.msg)
          }
        }
      }).get()
    },
    regionInit() {
      new this.$rest({
				gateway: 'agrobiz',
        service: "agrobiz-uc",
        url: "/v1.0/plus/union/region/lazy",
				query: {
					equal: {
            regionId: "0"
          }
				},
				action: (res)=>{
					if(res.data.code === 20000){
						const data = res.data.data
						this.regionArray = data
					} else {
						this.$Message.error({
              content: res.data.msg
            })
					}
				}
			}).get()
    },
		loadRegion(item, callback) {
			item.loading = true
			new this.$rest({
				gateway: 'agrobiz',
        service: "agrobiz-uc",
        url: "/v1.0/plus/union/region/lazy",
				query: {
					equal: {
            regionId: item.id,
            regionLevel: 5
          }
				},
				action: (res)=>{
					if(res.data.code === 20000){
						const data = res.data.data
						item.children = data
						item.loading = false
            callback()
					} else {
						this.$Message.error({
              content: res.data.msg
            })
					}
				}
			}).get()
		},
    regionSelector(value, data) {
      this.dataForm.userPlus.provinceCode = data[0].value
      this.dataForm.userPlus.provinceName = data[0].label
      this.dataForm.userPlus.cityCode = data[1].value
      this.dataForm.userPlus.cityName = data[1].label
      this.dataForm.userPlus.countyCode = data[2].value
      this.dataForm.userPlus.countyName = data[2].label
      this.dataForm.userPlus.townshipCode = data[3].value
      this.dataForm.userPlus.townshipName = data[3].label
      this.dataForm.userPlus.villageCode = data[4].value
      this.dataForm.userPlus.villageName = data[4].label
      //加载村组
      this.villageGroupInit(data[4].value)
		},
    villageGroupInit(id) {
      new this.$rest({
        gateway: 'agrobiz',
        service: 'agrobiz-uc',
        url: '/v1.0/village/group/page',
        query: {
          pageIndex: 1,
          pageSize: 99,
          tenant: false,
          equal: {
            regionCode: id
          }
        },
        action: (res) => {
          if (res.data.code === 20000) {
            this.villageGroupArray = res.data.data.list
          } else {
            this.$Message.error(res.data.msg)
          }
        }
      }).get()
    },
  }
}
</script>
<style lang="less" scoped>
.layer_top {
  display: flex; flex-wrap: wrap;
}
.name-area {
  padding: 3px 0; width: 33%;
}
.names {
  width: 100px; font-weight: 800; display: inline-block;
}
.imgs {
  width: 100px;
}
.yings {
  vertical-align: top;
}
</style>